/**
 * Created by Administrator on 2019/1/28.
 */
module.exports = {
  getCookie: str => {
    if (!str) return;
    let params = str.split(';'),
      cookieObj = {},
      cookie = '';
    params.forEach(item => {
      const param = item.split('=');
      // 有个httpOnly字段不知道是干嘛的,直接去掉
      if (param[0].indexOf('HttpOnly') > -1) {
        param[0] = param[0].replace('HttpOnly,', '');
      }
      cookie += (param[0] + '=' + decodeURIComponent(param[1]) + ';');
    });
    return cookie;
  },

  urlParam: url => {
    const theRequest = {};
    if (url.indexOf('?') != -1) {
      const str = url.substr(url.indexOf('?') + 1);
      strs = str.split('&');
      for (let i = 0, len = strs.length; i < len; i++) {
        param = strs[i].split('=');
        theRequest[param[0]] = decodeURIComponent(param[1]);
      }
    }
    return theRequest;
  },

  formatDate: (date, fmt) => {
    if (!fmt || !date) return; // 参数不存在的话不执行函数
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    const o = {
      'M+': date.getMonth() + 1, // 月份,由于月份从0开始，所以要加1
      'd+': date.getDate(), // 天
      'h+': date.getHours(), // 小时
      'm+': date.getMinutes(), // 分钟
      's+': date.getSeconds(), // 秒钟
    };

    const week = {
      0: '日',
      1: '一',
      2: '二',
      3: '三',
      4: '四',
      5: '五',
      6: '六',
    };

    if (/(E+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[date.getDay() + '']);
    }
    for (const k in o) {
      if (new RegExp(`(${k})`).test(fmt)) { // 注意RegExp.$1匹配的是正则表达式的第一个子串，凡是被括号包裹起来的才是子串。
        const str = o[k] + '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
      }
    }
    return fmt;
  },

  parseTime(time, cFormat) {
    if (arguments.length === 0) {
      return null;
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
    let date;
    if (typeof time === 'object') {
      date = time;
    } else {
      if (('' + time).length === 10) time = parseInt(time) * 1000;
      date = new Date(time);
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay(),
    };
    const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
      let value = formatObj[key];
      if (key === 'a') return [ '一', '二', '三', '四', '五', '六', '日' ][value - 1];
      if (result.length > 0 && value < 10) {
        value = '0' + value;
      }
      return value || 0;
    });
    return timeStr;
  },

  getDaysByDateString: (date1, date2) => {
    const startDate = Date.parse(date1.replace('/-/g', '/'));
    const endDate = Date.parse(date2.replace('/-/g', '/'));
    const diffDate = (endDate - startDate) + 1 * 24 * 60 * 60 * 1000;
    const days = diffDate / (1 * 24 * 60 * 60 * 1000);
    return days;
  },
};
